//------------------------------------------------------------------------------------------------------
//   _____     _______ ____  _     ___  ____  ____    _____ _   _  ____ ___ _   _ _____   ______  ___ _ 
//  / _ \ \   / / ____|  _ \| |   / _ \|  _ \|  _ \  | ____| \ | |/ ___|_ _| \ | | ____| |  _ \ \/ / / |
// | | | \ \ / /|  _| | |_) | |  | | | | |_) | | | | |  _| |  \| | |  _ | ||  \| |  _|   | | | \  /| | |
// | |_| |\ V / | |___|  _ <| |__| |_| |  _ <| |_| | | |___| |\  | |_| || || |\  | |___  | |_| /  \| | |
//  \___/  \_/  |_____|_| \_\_____\___/|_| \_\____/  |_____|_| \_|\____|___|_| \_|_____| |____/_/\_\_|_|
//
// Overlord Engine v1.82
// Copyright Overlord Thomas Goussaert & Overlord Brecht Kets
// http://www.digitalartsandentertainment.com/
//------------------------------------------------------------------------------------------------------
//Precompiled Header [ALWAYS ON TOP IN CPP]
#include "Base\stdafx.h"

#include "FurShader.h"
#include "Base/GeneralStructs.h"
#include "Diagnostics/Logger.h"
#include "Content/ContentManager.h"
#include "Graphics/TextureData.h"

ID3DX11EffectShaderResourceVariable* FurShader::m_pDiffuseSRVvariable = nullptr;
ID3DX11EffectShaderResourceVariable* FurShader::m_pDotSRVvariable = nullptr;
ID3DX11EffectShaderResourceVariable* FurShader::m_pMaskSRVvariable = nullptr;
ID3DX11EffectScalarVariable* FurShader::m_pGravitySRVvariable = nullptr;
ID3DX11EffectScalarVariable* FurShader::m_pFurLengthSRVvariable = nullptr;
ID3DX11EffectScalarVariable* FurShader::m_pFurUVSRVvariable = nullptr;

FurShader::FurShader() : Material(L"./Resources/Effects/MyFur.fx"),
	m_pDiffuseTexture(nullptr), m_pDotTexture(nullptr), m_pMaskTexture(nullptr), m_Gravity(0)
{
}


FurShader::~FurShader()
{

}

void FurShader::SetDiffuseTexture(const wstring& assetFile)
{
	m_pDiffuseTexture = ContentManager::Load<TextureData>(assetFile);
}

void FurShader::SetDotTexture(const wstring& assetFile)
{
	m_pDotTexture = ContentManager::Load<TextureData>(assetFile);
}

void FurShader::SetmaskTexture(const wstring& assetFile)
{
	m_pMaskTexture = ContentManager::Load<TextureData>(assetFile);
}

void FurShader::SetGravity(float gravity)
{
	m_Gravity = gravity;
}

void FurShader::SetLength(float length)
{
	m_FurLength = length;
}

void FurShader::SetUVSize(float size)
{
	m_FurUV = size;
}

void FurShader::LoadEffectVariables()
{
	if (!m_pDiffuseSRVvariable)
	{
		m_pDiffuseSRVvariable = m_pEffect->GetVariableByName("m_TextureDiffuse")->AsShaderResource();
		if (!m_pDiffuseSRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_TextureDiffuse\' variable not found!");
			m_pDiffuseSRVvariable = nullptr;
		}
	}

	if (!m_pDotSRVvariable)
	{
		m_pDotSRVvariable = m_pEffect->GetVariableByName("m_TextureFurDensity")->AsShaderResource();
		if (!m_pDotSRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_TextureFurDensity\' variable not found!");
			m_pDotSRVvariable = nullptr;
		}
	}

	if (!m_pMaskSRVvariable)
	{
		m_pMaskSRVvariable = m_pEffect->GetVariableByName("m_TextureMask")->AsShaderResource();
		if (!m_pMaskSRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_TextureMask\' variable not found!");
			m_pMaskSRVvariable = nullptr;
		}
	}

	if (!m_pGravitySRVvariable)
	{
		m_pGravitySRVvariable = m_pEffect->GetVariableByName("m_Gravity")->AsScalar();
		if (!m_pGravitySRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_Gravity\' variable not found!");
			m_pGravitySRVvariable = nullptr;
		}
	}

	if (!m_pFurLengthSRVvariable)
	{
		m_pFurLengthSRVvariable = m_pEffect->GetVariableByName("m_FurLength")->AsScalar();
		if (!m_pFurLengthSRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_FurLength\' variable not found!");
			m_pFurLengthSRVvariable = nullptr;
		}
	}

	if (!m_pFurUVSRVvariable)
	{
		m_pFurUVSRVvariable = m_pEffect->GetVariableByName("m_FurUV")->AsScalar();
		if (!m_pFurUVSRVvariable->IsValid())
		{
			Logger::LogWarning(L"DiffuseMaterial::LoadEffectVariables() > \'m_FurUV\' variable not found!");
			m_pFurUVSRVvariable = nullptr;
		}
	}
}

void FurShader::UpdateEffectVariables(const GameContext& gameContext, ModelComponent* pModelComponent)
{
	UNREFERENCED_PARAMETER(gameContext);
	UNREFERENCED_PARAMETER(pModelComponent);

	if (m_pDiffuseTexture && m_pDiffuseSRVvariable)
	{
		m_pDiffuseSRVvariable->SetResource(m_pDiffuseTexture->GetShaderResourceView());
	}

	if (m_pDotTexture && m_pDotSRVvariable)
	{
		m_pDotSRVvariable->SetResource(m_pDotTexture->GetShaderResourceView());
	}

	if (m_pMaskTexture && m_pMaskSRVvariable)
	{
		m_pMaskSRVvariable->SetResource(m_pMaskTexture->GetShaderResourceView());
	}

	if (m_pGravitySRVvariable)
	{
		m_pGravitySRVvariable->SetFloat(m_Gravity);
	}

	if (m_pFurLengthSRVvariable)
	{
		m_pFurLengthSRVvariable->SetFloat(m_FurLength);
	}

	if (m_pFurUVSRVvariable)
	{
		m_pFurUVSRVvariable->SetFloat(m_FurUV);
	}
}
